package com.hmall.api.config;

import com.hmall.api.fallback.*;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

public class DefaultFeignConfig {

    //配置feign的日志级别
    @Bean
    public Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }

    //feign请求拦截器
//    @Bean
//    public RequestInterceptor requestInterceptor(){
//        return requestTemplate -> {
//            Long userId = UserContext.getUser();
//            if (userId != null){
//                requestTemplate.header("user-info", userId.toString());
//            }
//        };
//    }
    //注册商品的熔断降级处理类

    //注册商品的熔断降级处理类
    @Bean
    public ItemClientFallback itemClientFallback(){
        return new ItemClientFallback();
    }
    //注册 CartClientFallback
    @Bean
    public CartClientFallback cartClientFallback(){
        return new CartClientFallback();
    }
    // 注册 TradeClientFallback
    @Bean
    public TradeClientFallback tradeClientFallback(){
        return new TradeClientFallback();
    }
    //注册 UserClientFallback
    @Bean
    public UserClientFallback userClientFallback(){
        return new UserClientFallback();
    }
    @Bean
    public PayClientFallback payClientFallback(){
        return new PayClientFallback();
    }


    //定义feign请求拦截器，设置用户信息
    @Bean
    public RequestInterceptor requestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                Long userId = UserContext.getUser();
                if(userId!= null){
                    template.header("user-info", userId.toString());
                }
            }
        };
    }
}
